package nuist.hanniko.stu.webbackend.service.impls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import nuist.hanniko.stu.webbackend.dao.mapper.HistoryRecordMapper;
import nuist.hanniko.stu.webbackend.model.LwM2m.LwM2MResourceValue;
import nuist.hanniko.stu.webbackend.service.DeviceRecordService;
import nuist.hanniko.stu.webbackend.service.DeviceRecordServiceWithPeriod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class HistoryRecordServiceImpl implements DeviceRecordService, DeviceRecordServiceWithPeriod {

    @Autowired
    private HistoryRecordMapper historyRecordMapper;

    @Override
    public JSONArray getResourceRecord(String deviceId, String objectId, String resourceId) {
        List<LwM2MResourceValue> resource = historyRecordMapper.getResource(deviceId, objectId, resourceId);
        return (JSONArray) JSON.toJSON(resource);
    }

    @Override
    public JSONArray getObjectRecord(String deviceId, String objectId) {
        List<LwM2MResourceValue> resources = historyRecordMapper.getByDeviceIdAndObjectId(deviceId, objectId);
        return (JSONArray) JSON.toJSON(resources);
    }

    @Override
    public JSONArray getDeviceRecord(String deviceId) {
        List<LwM2MResourceValue> resources = historyRecordMapper.getByDeviceId(deviceId);
        return (JSONArray) JSON.toJSON(resources);
    }

    @Override
    public JSONArray getDeviceRecordWithinPeriod(String deviceId, Date begin, Date end) {
        List<LwM2MResourceValue> resources = historyRecordMapper.getByDeviceIdWithinPeriod(deviceId, begin, end);
        return (JSONArray) JSON.toJSON(resources);
    }

    @Override
    public JSONArray getResourceRecordWithinPeriod(String deviceId, String objectId, String resourceId,
                                                   Date begin, Date end) {
        List<LwM2MResourceValue> resources =
                historyRecordMapper.getResourceWithinPeriod(deviceId, objectId, resourceId,begin, end);
        return (JSONArray) JSON.toJSON(resources);
    }

    @Override
    public JSONArray getObjectRecordWithinPeriod(String deviceId, String objectId,
                                                 Date begin, Date end) {
        List<LwM2MResourceValue> resources =
                historyRecordMapper.getByDeviceIdAndObjectIdWithinPeriod(deviceId, objectId,begin, end);
        return (JSONArray) JSON.toJSON(resources);
    }

    @Override
    public int addRecord(LwM2MResourceValue value) {
        try {
            historyRecordMapper.insert(value);
        } catch (Exception e) {
            return FAILURE;
        }
        return SUCCESS;
    }

    @Override
    public int addRecord(List<LwM2MResourceValue> valueList) {
        try {
            historyRecordMapper.insertBatch(valueList);
        } catch (Exception e) {
            return FAILURE;
        }
        return SUCCESS;
    }
}
